/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.agent.controller.shared;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import ca.usherbrooke.agent.controller.BasicAgentLauncher;
import ca.usherbrooke.behaviours.BasicCyclicBehaviour;
import ca.usherbrooke.utils.ExceptionTool;

/**
 * @author Cody Stoutenburg
 * 
 */
public class ExternalBehaviours extends
		BasicCyclicBehaviour<BasicAgentLauncher> {

	private static final long serialVersionUID = 1L;

	private final List<IBehaviour<?>> allBehaviour;
	private final List<IBehaviour<?>> allWorldChangedBehaviour;

	private Long previousTime;

	/**
	 * @param a
	 */
	public ExternalBehaviours(BasicAgentLauncher a) {
		super(a);
		this.allBehaviour = new ArrayList<IBehaviour<?>>();
		allWorldChangedBehaviour = new ArrayList<IBehaviour<?>>();
		previousTime = 0L;
	}

	public void addBehaviour(IBehaviour<?> behaviour) {
		this.allBehaviour.add(behaviour);
	}

	public void addBehaviour(IBehaviourOnWorldChanged<?> behaviour) {
		this.allWorldChangedBehaviour.add(behaviour);
	}

	@Override
	public void action() {
		// Do all behaviour
		executeBehaviourList(allBehaviour);

		// Do World changed behaviour
		Long currentTime = this.getAgent().getWorld().getRealTimeElapse();
		if (!currentTime.equals(previousTime)) {
			previousTime = currentTime;

			executeBehaviourList(allWorldChangedBehaviour);
		}
	}

	private void executeBehaviourList(List<IBehaviour<?>> execute) {
		Iterator<IBehaviour<?>> itt = execute.iterator();
		while (itt.hasNext()) {
			Boolean isDone = false;

			IBehaviour<?> behaviour = itt.next();
			try {
				behaviour.action();
			} catch (Exception ex) {
				// if the user as an exception
				// System.err.println("An error occur in entity "
				// + getAgent().getEntityGUID() + ", in behaviour "
				// + behaviour.getClass().getName());
				// ex.printStackTrace();

				ExceptionTool.showError("An error occur in entity "
						+ getAgent().getEntityGUID() + ", in behaviour "
						+ behaviour.getClass().getName(), ex);
				isDone = true;
			}

			try {
				isDone = behaviour.done();
			} catch (Exception ex) {
				// if the user as an exception
				// System.err.println("An error occur in entity "
				// + getAgent().getEntityGUID() + ", in behaviour "
				// + behaviour.getClass().getName() + " on done");
				// ex.printStackTrace();

				ExceptionTool.showError("An error occur in entity "
						+ getAgent().getEntityGUID() + ", in behaviour "
						+ behaviour.getClass().getName(), ex);
				isDone = true;
			}
			if (isDone) {
				// remove the behaviour
				itt.remove();
			}
		}
	}
}
